package com.fh.controller.system.dictionaries;

import com.fh.controller.base.BaseController;
import com.fh.model.Page;
import com.fh.service.cuijimanage.CuijimanageServiceImpl;
import com.fh.util.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fh.model.system.SystemDictionary;
import com.fh.model.system.SystemDictionaryItem;
import com.fh.query.SystemDictionaryItemQueryObject;
import com.fh.query.SystemDictionaryQueryObject;
import com.fh.service.system.dictionaries.ISystemDictionaryService;
import com.fh.util.ResultJSON;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SystemDictionaryController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(SystemDictionaryController.class);

    @Autowired
    private CuijimanageServiceImpl cuijimanageServiceImpl;

    @Autowired
	private ISystemDictionaryService service;
	/**
	 * 目录的高级查询
	 * 
	 * @param qo
	 * @param model
	 * @return
	 */
	@RequestMapping("systemDictionary_list")
	public String dicList(@ModelAttribute("qo") SystemDictionaryQueryObject qo,Model model) {
		model.addAttribute("pageResult", service.queryDis(qo));
		return "system/dictionaries/systemDictionary_list";
	}
	/**
	 * 新增或更新一条数据
	 * @param dic
	 * @return
	 */
	@RequestMapping("systemDictionary_update")
	@ResponseBody
	public ResultJSON save(SystemDictionary dic){
		service.saveOrUpdate(dic);
		return new ResultJSON();
	}
	/**
	 * 数据字典明细
	 * @return
	 */
	@RequestMapping("systemDictionaryItem_list")
	public String systemDictionaryItem_list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo,Model model) {
		model.addAttribute("pageResult", service.queryItem(qo));
		model.addAttribute("systemDictionaryGroups", service.selectAll());
		return "system/dictionaries/systemDictionaryItem_list";
	}
	
	@RequestMapping("systemDictionaryItem_update")
	@ResponseBody
	public ResultJSON saveItem(SystemDictionaryItem item){
		service.saveOrUpdateItem(item);
		return new ResultJSON();
	}
    @RequestMapping("systemDictionaryItem_delete1")
    @ResponseBody
    public int deleteItem1(long id){
        int i = 0;
        try {
         i =   service.deleteByisDel1(id);
        }catch (Exception e){
            e.printStackTrace();
            return  i ;
        }
        return i;
    }
    @RequestMapping("systemDictionaryItem_delete3")
    @ResponseBody
    public int deleteItem3(long id){
        int i = 0;
        try {
            i =   service.deleteByisDel3(id);
        }catch (Exception e){
            e.printStackTrace();
            return  i ;
        }
        return i;
    }
    /**
     * 跳转到demopage数据字典测试页面，仅供开发自己测试使用
     * @return
     */
    @RequestMapping("todemopage")
    public ModelAndView todemopage(HttpServletRequest request,Page page){
        ModelAndView mv = new ModelAndView();
        List<SystemDictionaryItem> overdueDurationList = service.selectItemBySn("overdueDuration");
        List<SystemDictionaryItem> callResultList = service.selectItemBySn("callResult");
        mv.addObject("overdueDurationList",overdueDurationList);
        mv.addObject("callResultList",callResultList);
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String,Object> map = new HashMap<>();
        map.put("CALLLOANNID", null);
        map.put("repaymentId", null);
        map.put("callResult", null);
        map.put("callUser", null);
        map.put("telephone", null);
        map.put("CONTIDCARD", null);
        map.put("overdueDuration", null);
        map.put("overdueDate", null);
        map.put("overdueFeeMin", null);
        map.put("overdueFeeMax", null);
        map.put("ruanDayMin",null);
        map.put("ruanDayMax",null);
        map.put("callTimeMin",null);
        map.put("callTimeMax",null);
        map.put("overdueAmountMax", null);
        map.put("overdueAmountMin", null);
        map.put("huankuanDayMin",null);
        map.put("huankuanDayMax",null);
        map.put("sortFind", "a.status,a.create_time DESC");
        pd.put("map", map);
        page.setPd(pd);
        List<PageData>  anjianlist = null;
        //anjianlist = cuijimanageServiceImpl.anjianlistPage(page);
        try {
               anjianlist = cuijimanageServiceImpl.anjianlistPage(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        mv.addObject("anjianlist",anjianlist);
        mv.setViewName("system/dictionaries/demopage");
        return  mv;
    }
}
